package com.tea.room.service.door.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tea.room.dao.WeChatTokenDao;
import com.tea.room.model.WeChatToken;
import com.tea.room.service.door.DoorService;
import com.tea.room.util.ConfigUtil;
import com.tea.room.util.HttpClient;
import com.tea.room.util.JsonUtil;

@Service
public class DoorServiceImpl implements DoorService {

    private static Logger logger = LoggerFactory.getLogger(DoorServiceImpl.class);
    private static Properties pro = ConfigUtil.getProperties("door.properties");
    private static String contentType = "application/json";
    
    @Autowired
    private WeChatTokenDao weChatTokenDao;
    
    @Override
    public String getAccessToken() {
        String appid = pro.get("APPID").toString();
        String appsecret = pro.get("APPSECRET").toString();
        String url = pro.get("TOKENURL").toString();
        Map<String, String> map = new HashMap<String, String>();
        map.put("appid", appid);// 登录用户名
        map.put("appsecret", appsecret);// 登录用户口令，填写密码
        String params = JsonUtil.mapToJsonObject(map).toJSONString();
        HttpClient client = HttpClient.getInstance();
        logger.info("token url:" + url);
        logger.info("token params:" + params);
        String post = client.sendHttpPost(url, params, contentType);
        logger.info("token return:" + post);
        return post;
    }

    @Override
    public void insertAccessToken(WeChatToken wc) {
        weChatTokenDao.updateDoorToken(wc);
        weChatTokenDao.insert(wc);
    }
    
    @Override
    public void removeAccessToken() {
        weChatTokenDao.delete(new WeChatToken());
    }
    
    /**
     * 获取token
     * @return
     */
    private String getToken(){
        WeChatToken token = weChatTokenDao.queryDoorToken();
        return token.getAccess_token();
    }

    @Override
    public String getBuilding(Map<String, Object> param) {
        String token = getToken();
        String url = pro.get("BUILDINGURL").toString() + token;
        HttpClient client = HttpClient.getInstance();
        String params = JsonUtil.mapToJsonObject(param).toJSONString();
        String post = client.sendHttpPost(url, params, contentType);
        logger.info("getBuilding return:" + post);
        return post;
    }

    @Override
    public String getFloor(Map<String, Object> param) {
        String token = getToken();
        String url = pro.get("FLOORURL").toString() + token;
        HttpClient client = HttpClient.getInstance();
        String params = JsonUtil.mapToJsonObject(param).toJSONString();
        String post = client.sendHttpPost(url, params, contentType);
        logger.info("getFloor return:" + post);
        return post;
    }

    @Override
    public String getRoomList(Map<String, Object> param) {
        String token = getToken();
        String url = pro.get("ROOMLISTURL").toString() + token;
        HttpClient client = HttpClient.getInstance();
        String params = JsonUtil.mapToJsonObject(param).toJSONString();
        String post = client.sendHttpPost(url, params, contentType);
        logger.info("getRoomList return:" + post);
        return post;
    }

    @Override
    public String getRoom(Map<String, Object> param) {
        String token = getToken();
        String url = pro.get("ROOMURL").toString() + token;
        HttpClient client = HttpClient.getInstance();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("building", param.get("building"));// 楼栋名字
        map.put("floor", param.get("floor"));// 楼层名字
        map.put("room", param.get("room"));// 房间名字
        String params = JsonUtil.mapToJsonObject(map).toJSONString();
        String post = client.sendHttpPost(url, params, contentType);
        logger.info("getRoom return:" + post);
        return post;
    }

    @Override
    public String getDoorlock(String placeId) {
        String token = getToken();
        String url = pro.get("DOORLOCKURL").toString() + token;
        HttpClient client = HttpClient.getInstance();
        Map<String, String> map = new HashMap<String, String>();
        map.put("placeId", placeId);// 房间id
        String params = JsonUtil.mapToJsonObject(map).toJSONString();
        String post = client.sendHttpPost(url, params, contentType);
        logger.info("getDoorlock return:" + post);
        return post;
    }

    @Override
    public String queryLockBattery(List<String> list) {
        String token = getToken();
        String url = pro.get("QUERYLOCKBATTERY").toString() + token;
        String contentType = "application/json";
        HttpClient client = HttpClient.getInstance();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("roomIds", list);// 房间id，数组
        String params = JsonUtil.mapToJsonObject(map).toJSONString();
        String post = client.sendHttpPost(url, params, contentType);
        logger.info("queryLockBattery return:" + post);
        return post;
    }

    @Override
    public String remoteUnlock(Map<String, Object> param) {
        String token = getToken();
        String url = pro.get("REMOTEUNLOCK").toString() + token;
        HttpClient client = HttpClient.getInstance();
        String params = JsonUtil.mapToJsonObject(param).toJSONString();
        String post = client.sendHttpPost(url, params, contentType);
        logger.info("remoteUnlock return:" + post);
        return post;
    }

    @Override
    public String queryOperationStatus(Map<String, Object> param) {
        String token = getToken();
        String url = pro.get("QUERYSTATUS").toString() + token;
        HttpClient client = HttpClient.getInstance();
        String params = JsonUtil.mapToJsonObject(param).toJSONString();
        String post = client.sendHttpPost(url, params, contentType);
        logger.info("queryOperationStatus return:" + post);
        return post;
    }

    @Override
    public String onlinePassword(Map<String, Object> param) {
        String token = getToken();
        String url = pro.get("ONLINEPASSWORD").toString() + token;
        HttpClient client = HttpClient.getInstance();
//        param.put("lockId", 3615);// 门锁id，通过查询门锁电池和信号状态 返回的ID    手动查，配置在数据库中
        param.put("holder", "张三");// 持有人，可选
        param.put("phoneCountry", "+86"); // 手机国际区号，国内为+86，其他国家按照相应区号填写
        param.put("phoneNumber", "17600110458");// 手机号
        param.put("subType", 4);// 密码类型， 1=永久，2=单次，4=限时
        param.put("canOpenAntilock", true);// 是否可以开反锁,true：可以开,false:不能开
        String params = JsonUtil.mapToJsonObject(param).toJSONString();
        logger.info("onlinePassword params:" + params);
        String post = client.sendHttpPost(url, params, contentType);
        logger.info("onlinePassword return:" + post);
        return post;
    }

    @Override
    public String queryPasswordStatus(String operationId, String hardwareId) {
        String token = getToken();
        String url = pro.get("QUERYONLINEPASSWORD").toString() + token;
        HttpClient client = HttpClient.getInstance();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("requestType", 1);// 请求操作的类型，下发在线密码为1
        map.put("operationId", operationId);// 操作id，下发在线密码时返回
        map.put("hardwareId", hardwareId);// 门锁的硬件id
        String params = JsonUtil.mapToJsonObject(map).toJSONString();
        String post = client.sendHttpPost(url, params, contentType);
        logger.info("queryPasswordStatus return:" + post);
        return post;
    }
    
}
